import acroMLUtils from '../acroML.Utils.mjs';
let g_lastClassID=0;
let classMethods={};

function checkClassID(c){
  let isExists=true;
  let classID=c._acroml_classID;
  if (classID==undefined){
    isExists=false;
    classID=g_lastClassID;
    g_lastClassID++;
    c._acroml_classID=classID;
  }
  return {classID,isExists};
}

function getClassMethod(c){
  let classID=c._acroml_classID;
  let m=null;
  if (classID!=undefined) m=classMethods[classID];
  return {classID,method:m};
}

let acroMLClassMethod={
  register(c,m){
    if (typeof c=='string') classMethods[c]=m;
    else{
      if (c.name && (c.name!='Object')) classMethods[c.name]=m;
      else if (acroMLUtils.isObject(c)){
        let {classID,isExists}=checkClassID(c);
        if (!isExists) classMethods[classID]=m;
      }
    }
  },
  translateCom(t,coms){
    for(let i=0;i<coms.length;i++){
      let com=coms[i];
      let typeName,m;
      if (com.constructor){
        typeName=com.constructor.name;
        if (typeName && (typeName!='Object')) m=classMethods[typeName];
        else{
          let {classID,method}=getClassMethod(com.constructor);
          m=method;
        }
      }
      if (m) m(t,com);
    }
  },
  translateProp(t,obj,propName,propDefaultValue){
    if (!obj._acroDDKeys) obj._acroDDKeys={};
    let DDKey=obj._acroDDKeys[propName];
    if (!DDKey){
      DDKey=obj[propName];
      if (!DDKey) DDKey=propDefaultValue;
      //保存原始的DDKey
      obj._acroDDKeys[propName]=DDKey;
    }
    if (DDKey) obj[propName]=t(DDKey);
  }
}
export default acroMLClassMethod;